//
//  ZBSegmentedView.m
//  XZBProduct
//
//  Created by xzb on 2019/1/3.
//  Copyright © 2019 xzb. All rights reserved.
//

#import "ZBSegmentedView.h"
#import "ZBSegmentedTabButton.h"

@interface ZBSegmentedView ()

@property (nonatomic, strong) NSArray *titleImages;
@property (nonatomic, strong) NSArray *objects;

@end


@implementation ZBSegmentedView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self addSubView];
        [self addLayout];
        self.backgroundColor = [UIColor cyanColor];
    }
    return self;
}

#pragma mark - api
- (void)setTitleObjects:(NSArray *)objects
{
    NSArray *objs = [objects bk_select:^BOOL(id  _Nonnull obj) {
        if ([obj isKindOfClass:[UIImage class]]) {
            return YES;
        }
        if ([obj isKindOfClass:[NSString class]]) {
            return YES;
        }
        return NO;
    }];
    
    _objects = [objs copy];

    @weakify(self);
    self.titleImages = [objs bk_map:^id _Nonnull(id  _Nonnull obj) {
        if ([obj isKindOfClass:[NSString class]]) {
            @strongify(self);
            return [self getTitleImageWithText:obj];
        }
        return [self getTitleImageWithImage:obj];
    }];
    
    [self display];
}

- (void)display
{
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    ZBSegmentedTabButton *lastBtn = nil;
    
    for (NSInteger idx = 0; idx < self.titleImages.count ; idx ++ ) {
        UIImage *image = self.titleImages[idx];
        ZBSegmentedTabButton *tabButton = [[ZBSegmentedTabButton alloc] init];
        tabButton.normalImage = image;
        tabButton.index = idx;
        
        [self addSubview:tabButton];
        [tabButton mas_makeConstraints:^(MASConstraintMaker *make) {
            if (lastBtn) {
                make.left.equalTo(lastBtn.mas_right).offset(0);
            } else {
                make.left.offset(0);
            }
            make.top.bottom.offset(0);
            make.width.offset(image.size.width);
        }];
        lastBtn = tabButton;
        [tabButton addTarget:self action:@selector(tabButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

#pragma mark - event Response

- (void)tabButtonClick:(ZBSegmentedTabButton *)sender
{
    sender.selected = !sender.selected;
 
    NSLog(@"%ld",(long)sender.index);
    NSInteger index = sender.index;
    if ([self.delegate respondsToSelector:@selector(segmentedViewDidSelcted:object:)]) {
        [self.delegate segmentedViewDidSelcted:index object:self.objects[index]];
    }
}

#pragma mark - private methods

- (UIImage *)getTitleImageWithImage:(UIImage *)image
{
    CGFloat containerHegiht = 40;
    CGSize imageSize = CGSizeMake(image.size.width + 5 * 2, containerHegiht);
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawPath(context, kCGPathStroke);
    UIImage *contentImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return contentImage;
}

- (UIImage *)getTitleImageWithText:(NSString *)text
{
    CGFloat containerHegiht = 40;
    UIFont *textFont = [UIFont systemFontOfSize:14];
    UIColor *textColor = [UIColor blackColor];
    
    NSDictionary *textAttributedDict = @{
                                         NSFontAttributeName : textFont,
                                         NSForegroundColorAttributeName : textColor
                                         };
    CGSize textSize =  [text sizeWithAttributes:textAttributedDict];
    CGSize imageSize = CGSizeMake(textSize.width + 5 * 2, containerHegiht);
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawPath(context, kCGPathStroke);
    [text drawInRect:CGRectMake(5, (containerHegiht - textFont.lineHeight) * 0.5, textSize.width, textFont.lineHeight) withAttributes:textAttributedDict];
    UIImage *contentImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return contentImage;
}

- (void)addSubView
{
    
}

- (void)addLayout
{
    
}

#pragma mark - getter And setter


@end
